﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.EntityModel;
using RMS.DB;

namespace RMS.DB
{
    public class DBController
    {
        internal dataEntities DataEntities;
        internal bool _transactionFlag;
        public DBController()
        {
            if (DataEntities == null)
                DataEntities = new dataEntities();
        }
        public dataEntities DataContext
        {
            get
            {
                return DataEntities ?? (DataEntities = new dataEntities(ConfigurationSettings.AppSettings["dataEntities"]));
            }
        }

        public IDbTransaction BeginTransaction()
        {
            _transactionFlag = true;
            // Start _trans
            DataEntities.Connection.Open();
            return DataEntities.Connection.BeginTransaction();
        }

        public void CommitTransaction(IDbTransaction transaction)
        {
            _transactionFlag = false;
            // Start _trans                        
            if (transaction == null || DataEntities.Connection.State == ConnectionState.Closed)
                return;
            transaction.Commit();
            transaction.Dispose();
        }

        public void RollbackTransaction(IDbTransaction transaction)
        {
            _transactionFlag = false;
            // Start _trans
            if (transaction == null || DataEntities.Connection.State == ConnectionState.Closed)
                return;
            transaction.Rollback();
            transaction.Dispose();
            transaction = null;
        }
    }
}
